package com.example.dddsample.application.service;

import com.example.commom.model.ApiResult;
import com.example.commom.model.PageRows;
import com.example.dddsample.application.api.response.CustomerResponseDTO;
import com.example.dddsample.application.assembler.CustomerAssembler;
import com.example.dddsample.application.event.CustomerKafkaProducerEvent;
import com.example.dddsample.application.external.CustomerExtClient;
import com.example.dddsample.domain.entity.CustomerAggregate;
import com.example.dddsample.domain.service.CustomerService;
import com.example.dddsample.domain.vo.CustomerQueryBaseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class CustomerAppService {

    private final CustomerService customerService;

    private final CustomerAssembler customerAssembler;

    private final CustomerExtClient customerExtClient;

    private final CustomerKafkaProducerEvent customerKafkaProducerEvent;

    /**
     * @param customerService
     * @param customerAssembler
     */
    public CustomerAppService(CustomerService customerService,
                              CustomerAssembler customerAssembler,
                              CustomerExtClient customerExtClient,
                              CustomerKafkaProducerEvent customerKafkaProducerEvent) {
        this.customerService = customerService;
        this.customerAssembler = customerAssembler;
        this.customerExtClient = customerExtClient;
        this.customerKafkaProducerEvent = customerKafkaProducerEvent;
    }

    /**
     * @param custId
     * @return CustomerResponseDTO
     */
    public CustomerResponseDTO getCustomer(String custId) {
        //CustomerAggregate customerAggregate=new CustomerAggregate();
        CustomerAggregate customerAggregate = customerService.getCustomer(custId);
        CustomerResponseDTO customerResponseDTO = customerAssembler.assemblerFromCustomerAggregate(customerAggregate);
        //todo 偷懒了应该使用注解的方式去实现,后面要改
        customerKafkaProducerEvent.customerOperProducerProperties(customerResponseDTO);
        return customerResponseDTO;
    }


    public PageRows getPageCustomer(int pageNum, int pageSize, String custName) {
        CustomerQueryBaseVO customerQueryBaseVO = customerAssembler.assemblerFormCustomerPage(custName);
        return customerService.getPageCustomer(customerQueryBaseVO, pageNum, pageSize);
    }

    /**
     * getFeignClientCustomer
     *
     * @param custId
     * @return ApiResult<CustomerResponseDTO>
     */
    public ApiResult<CustomerResponseDTO> getFeignClientCustomer(String custId) {
        return customerExtClient.getCustomer(custId);
    }
}
